<template>
  <div class="app-container">
    <div class="app-container_conter">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="110px" style="display: flex;justify-content: space-between">
      <el-form-item label="企业名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item style="width: 150px;text-align: right">
        <el-button type="primary"  size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="info" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            style="background: #0793FF;color:#FFFFFF "
            size="mini"
            @click="handleAdd"
            v-hasPermi="['insurance:ApplicantInformation:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
            type="info"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['insurance:ApplicantInformation:remove']"
        >删除</el-button>
      </el-col>
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table v-loading="loading" :data="employerList" @selection-change="handleSelectionChange"  :header-cell-style="tableHeaderColor">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="企业名称" align="center" prop="name" fixed min-width="170px" />
      <el-table-column label="统一社会信息代码" align="center" prop="credit_no" min-width="180px" />
      <el-table-column label="联系人" align="center" prop="contact" min-width="100px" />
      <el-table-column label="联系方式" align="center" prop="phone" min-width="130px" />
      <el-table-column label="地址" align="center" prop="mail_address" min-width="180px" />
      <el-table-column label="邮编" align="center" prop="email_code" min-width="120px" />
      <el-table-column label="邮箱" align="center" prop="email" min-width="130px" />

      <el-table-column label="操作" align="center" fixed="right" min-width="120px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['insurance:ApplicantInformation:edit']"
          >修改</el-button>
          <el-button
              style="color:#FF0000 "
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
              v-hasPermi="['insurance:ApplicantInformation:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    </div>
    <pagination
        style="background: transparent"
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改用工单位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="企业名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="credit_no">
          <el-input v-model="form.credit_no" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="通讯地址" prop="mail_address">
          <el-input v-model="form.mail_address" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="邮编" prop="email_code">
          <el-input v-model="form.email_code" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="info" @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {get_policy_holder,add_edit_company,get_edit_company} from '@/api/policyHolder/ApplicantInformation'
export default {
  name: "index",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用工单位表格数据
      employerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        user_id:null,
        role_id:null,
        type:1,//投保单位
        name: null,
      },
      // 表单参数
      form: {

      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        credit_no: [
          { required: true, message: "统一社会信用代码不能为空", trigger: "blur" },
          {pattern:/^([0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}|[1-9]\d{14})$/, message: '请输入正确的社会信用代码'}
        ],
        contact: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "电话不能为空", trigger: "blur" },
          {pattern: /^(1[3|4|5|6|7|8|9])\d{9}$|^0\d{2,3}-?\d{7,8}$/, message: '请输入正确的联系人电话'}
        ],
        mail_address:[
          { required: true, message: "通讯地址不能为空", trigger: "blur" }
        ],
        email_code:[
          { required: true, message: "邮编不能为空", trigger: "blur" },
          {pattern: /[1-9]\d{5}(?!\d)/, message: '请输入正确的邮编'}
        ],
        email:[
          { required: true, message: '邮箱号码', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱号码', trigger: ['blur', 'change'] }
        ],

      }
    };
  },
  created() {
    this.queryParams.user_id = JSON.parse(localStorage.getItem('userInfo')).userId;
    var info=JSON.parse(localStorage.getItem('userInfo'));
    if (info.roles.length!=0){
      this.queryParams.role_id=info.roles[0].roleId
    }
    this.getList();

  },
  methods: {
    /* 修改表头颜色 */
    tableHeaderColor({ rowIndex }) {
      if (rowIndex === 0) {
        return 'background-color: #304156;color: #fff;font-weight: 400;font-family: Microsoft YaHei;'
      }
    },
    /** 查询用工单位列表 */
    getList() {
      this.loading = true;
      // console.log('queryParams',this.queryParams)
      get_policy_holder(this.queryParams).then(response => {
        this.loading = false;
        // console.log('查询所有数据',response)
        if(response.code==1){
          this.employerList = response.data.list;
          this.total = response.data.count;
        }


      });
    },


    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        user_id:this.queryParams.user_id,// 的当前登录人id
        id: null,//数据id，添加时传空，编辑时传具体id
        type:1,//1投保单位  2被投保单位
        name: null,//公司名称
        credit_no:null,// 统一社会信用代码
        contact: null,//联系人
        phone: null,//联系人电话
        mail_address: null,//通讯地址
        email_code: null,//邮编
        email: null,//邮箱号码
        industry:null,// 行业类别
        business_scope:null,// 营业范围
        business_img:null,// 营业执照
        remark:null,// 备注
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加投保人";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      var obj={
        id:id,
        type:1
      }
      get_edit_company(obj).then(response => {
        console.log('详情',response)
        this.form = response.data;
        this.open = true;
        this.title = "投保人编辑";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log('form',this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            add_edit_company(this.form).then(response => {
              // console.log('response编辑',response)
              if(response.code==1){
                this.msgSuccess(response.msg);
                this.open = false;
                this.getList();
              }

            });
          } else {
            add_edit_company(this.form).then(response => {
              // console.log('response添加',response)
              if(response.code==1){
                this.msgSuccess(response.msg);
                this.open = false;
                this.getList();
              }

            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      var obj={
        id:ids,
        type:2,//1获取  2删除
      }
      console.log('obj',obj)
      var that=this;
      this.$confirm('是否确认删除用工单位编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        get_edit_company(obj).then(res=>{
          if(res.code==1){
            that.msgSuccess(res.msg);
            that.getList();
          }
          console.log('删除',res)
        })
      }).then(() => {


      })
    },

  }
};
</script>
<style scoped>
.app-container_conter{
  background: #F8FBFF;
  padding:30px;
  border-radius: 10px;
  box-sizing: border-box;
}
</style>

